﻿Module mPlayers

    Public Structure Point
        Dim X As Integer
        Dim Y As Integer
    End Structure

    Public Structure MirMapPlayer
        Dim conn As Connection
    End Structure

    Public Sub UpdateLocalPlayers(ByVal updatetype As CharacterUpdateType, ByRef map As MirMap, ByRef Conn As Connection)

        If map.name = "" Then Exit Sub

        Dim reply As Packet
        
        Dim addreply As Packet
        addreply.PacketType = GamePacketType.CharacterAddUpdate
        Dim struct As Object
        Dim addstruct As CharacterAddInfo
        Dim addstruct2 As CharacterAddInfo

        addstruct.ID_CHARACTER = Conn.CharacterID
        addstruct.playerinfo = Conn.PlayerInfo
        addstruct.Positioninfo = Conn.PositionInfo
        addstruct.VitalPoints = Conn.VitalPoints

        Dim remreply As Packet
        remreply.PacketType = GamePacketType.CharacterRemoveUpdate

        Dim remstruct As CharacterID
        Dim remstruct2 As CharacterID

        remstruct.ID_CHARACTER = Conn.CharacterID

        Select Case updatetype
            Case CharacterUpdateType.Add
                reply.PacketType = GamePacketType.CharacterAddUpdate
                struct = addstruct
            Case CharacterUpdateType.Move
                reply.PacketType = GamePacketType.CharacterMoveUpdate
                struct = New CharacterMoveInfo
                struct.ID_CHARACTER = Conn.CharacterID
                struct.Positioninfo = Conn.PositionInfo
            Case CharacterUpdateType.Remove
                reply.PacketType = GamePacketType.CharacterRemoveUpdate
                struct = remstruct
            Case Else
                struct = Nothing
        End Select

        reply.PacketOpCode = PacketOpCode.Notify
        addreply.PacketOpCode = PacketOpCode.Notify
        remreply.PacketOpCode = PacketOpCode.Notify

        Dim movedir As Point = CalcNextStep(0, 0, Conn.PositionInfo.BYTE_DIRECTION)

        SyncLock map.objMapLock
            If map.Players.Count > 0 Then
                For Each plyr As MirMapPlayer In map.Players
                    If Not plyr.conn Is Conn Then

                        If IsEnteringPlayArea(movedir, Conn.PositionInfo.INT2_X, Conn.PositionInfo.INT2_Y, plyr.conn.PositionInfo.INT2_X, plyr.conn.PositionInfo.INT2_Y) Then

                            addreply.Username = plyr.conn.Username
                            Socket.SockServer.SendData(Protocol.MakePacket(addreply, addstruct, plyr.conn.Crypto), plyr.conn.Index)

                            addreply.Username = Conn.Username
                            addstruct2.ID_CHARACTER = plyr.conn.CharacterID
                            addstruct2.playerinfo = plyr.conn.PlayerInfo
                            addstruct2.Positioninfo = plyr.conn.PositionInfo
                            addstruct2.VitalPoints = plyr.conn.VitalPoints
                            Socket.SockServer.SendData(Protocol.MakePacket(addreply, addstruct2, Conn.Crypto), Conn.Index)

                        ElseIf IsLeavingPlayArea(movedir, Conn.PositionInfo.INT2_X, Conn.PositionInfo.INT2_Y, plyr.conn.PositionInfo.INT2_X, plyr.conn.PositionInfo.INT2_Y) Then

                            remreply.Username = plyr.conn.Username
                            Socket.SockServer.SendData(Protocol.MakePacket(remreply, remstruct, plyr.conn.Crypto), plyr.conn.Index)

                            remreply.Username = Conn.Username
                            remstruct2.ID_CHARACTER = plyr.conn.CharacterID
                            Socket.SockServer.SendData(Protocol.MakePacket(remreply, remstruct2, Conn.Crypto), Conn.Index)

                        Else

                            If IsInsidePlayArea(Conn.PositionInfo.INT2_X, Conn.PositionInfo.INT2_Y, plyr.conn.PositionInfo.INT2_X, plyr.conn.PositionInfo.INT2_Y) Then
                                reply.Username = plyr.conn.Username
                                Socket.SockServer.SendData(Protocol.MakePacket(reply, struct, plyr.conn.Crypto), plyr.conn.Index)
                            End If

                        End If

                        System.Windows.Forms.Application.DoEvents()

                    End If
                Next
            End If

        End SyncLock

        If updatetype <> CharacterUpdateType.Remove Then

            Dim mobaddreply As Packet
            Dim mobaddstruct2 As MobAddInfo

            Dim mobremreply As Packet
            Dim mobremstruct2 As MobRemoveInfo

            mobaddreply.PacketType = GamePacketType.MobAddUpdate
            mobaddreply.PacketOpCode = PacketOpCode.Notify
            mobaddreply.Username = Conn.Username

            mobremreply.PacketType = GamePacketType.MobRemoveUpdate
            mobremreply.PacketOpCode = PacketOpCode.Notify
            mobremreply.Username = Conn.Username

            Dim npcaddreply As Packet
            Dim npcaddstruct As NPCAddInfo

            npcaddreply.PacketType = GamePacketType.NPCAddUpdate
            npcaddreply.PacketOpCode = PacketOpCode.Notify
            npcaddreply.Username = Conn.Username

            SyncLock map.objMapLock

                For Each mob As MirMapMob In map.Mobs.mColMobs

                    If Not mob.ID_MOB = 0 Then

                        If IsEnteringPlayArea(movedir, Conn.PositionInfo.INT2_X, Conn.PositionInfo.INT2_Y, mob.Pos.INT2_X, mob.Pos.INT2_Y) Then

                            mobaddstruct2.ServerMobID = mob.ServerMobID
                            mobaddstruct2.INT2_SHAPE = mMobs.colMobDB("MON" & mob.ID_MOB).INT2_SHAPE
                            mobaddstruct2.Positioninfo = mob.Pos
                            mobaddstruct2.VitalPoints = mob.VitalPoints
                            mobaddstruct2.Info.Name = mMobs.colMobDB("MON" & mob.ID_MOB).STR_NAME
                            mobaddstruct2.Info.Level = mMobs.colMobDB("MON" & mob.ID_MOB).INT2_LEVEL
                            mobaddstruct2.Info.Avatar = mMobs.colMobDB("MON" & mob.ID_MOB).AvatarImg

                            Socket.SockServer.SendData(Protocol.MakePacket(mobaddreply, mobaddstruct2, Conn.Crypto), Conn.Index)

                        ElseIf IsLeavingPlayArea(movedir, Conn.PositionInfo.INT2_X, Conn.PositionInfo.INT2_Y, mob.Pos.INT2_X, mob.Pos.INT2_Y) Then

                            mobremstruct2.ServerMobID = mob.ServerMobID
                            Socket.SockServer.SendData(Protocol.MakePacket(mobremreply, mobremstruct2, Conn.Crypto), Conn.Index)
                        End If

                    End If

                Next

                For Each npc As NPC.mNPCList.MirMapNPC In map.NPCs.lstNPCS

                    If IsEnteringPlayArea(movedir, Conn.PositionInfo.INT2_X, Conn.PositionInfo.INT2_Y, npc.mX, npc.mY) Then

                        npcaddstruct.ID_NPC = npc.NPCID
                        npcaddstruct.Name = npc.mName
                        npcaddstruct.CMD = npc.mCMD
                        npcaddstruct.Shape = npc.mShape
                        npcaddstruct.X = npc.mX
                        npcaddstruct.Y = npc.mY

                        Socket.SockServer.SendData(Protocol.MakePacket(npcaddreply, npcaddstruct, Conn.Crypto), Conn.Index)

                    End If

                Next


            End SyncLock

            map.FloorItems.UpdatePlayerDueToMove(Conn, movedir)

        End If

    End Sub

    Public Function CalcNextStep(ByVal cx As Integer, ByVal cy As Integer, ByVal dir As Direction) As Point

        Dim ret As Point
        ret.X = cx
        ret.Y = cy

        Select Case dir
            Case Direction.N
                ret.Y -= 1
            Case Direction.NE
                ret.Y -= 1
                ret.X += 1
            Case Direction.E
                ret.X += 1
            Case Direction.SE
                ret.Y += 1
                ret.X += 1
            Case Direction.S
                ret.Y += 1
            Case Direction.SW
                ret.Y += 1
                ret.X -= 1
            Case Direction.W
                ret.X -= 1
            Case Direction.NW
                ret.Y -= 1
                ret.X -= 1
        End Select

        Return ret

    End Function

    Public Sub UpdatePlayer(ByRef Conn As Connection, ByRef map As MirMap, ByVal x As Integer, ByVal y As Integer)

        Dim reply As Packet
        reply.PacketType = GamePacketType.CharacterAddUpdate
        reply.PacketOpCode = PacketOpCode.Notify
        reply.Username = Conn.Username

        Dim struct As CharacterAddInfo

        SyncLock map.objMapLock

            If map.Players.Count > 0 Then

                For Each plyr As MirMapPlayer In map.Players
                    If Not plyr.conn Is Conn Then
                        If IsInsidePlayArea(x, y, plyr.conn.PositionInfo.INT2_X, plyr.conn.PositionInfo.INT2_Y) Then

                            struct.ID_CHARACTER = plyr.conn.CharacterID
                            struct.playerinfo = plyr.conn.PlayerInfo
                            struct.Positioninfo = plyr.conn.PositionInfo
                            struct.VitalPoints = plyr.conn.VitalPoints

                            Socket.SockServer.SendData(Protocol.MakePacket(reply, struct, Conn.Crypto), Conn.Index)
                            System.Windows.Forms.Application.DoEvents()

                        End If
                    End If
                Next

            End If

        End SyncLock

        reply.PacketType = GamePacketType.MobAddUpdate
        Dim struct2 As MobAddInfo

        Dim npcaddreply As Packet
        Dim npcaddstruct As NPCAddInfo

        npcaddreply.PacketType = GamePacketType.NPCAddUpdate
        npcaddreply.PacketOpCode = PacketOpCode.Notify
        npcaddreply.Username = Conn.Username

        SyncLock map.objMapLock

            For Each Mob As MirMapMob In map.Mobs.mColMobs

                If Mob.ID_MOB <> 0 Then

                    If IsInsidePlayArea(x, y, Mob.Pos.INT2_X, Mob.Pos.INT2_Y) Then

                        struct2 = New MobAddInfo

                        struct2.ServerMobID = Mob.ServerMobID
                        struct2.INT2_SHAPE = mMobs.colMobDB("MON" & Mob.ID_MOB).INT2_SHAPE
                        struct2.Positioninfo = Mob.Pos
                        struct2.VitalPoints = Mob.VitalPoints
                        struct2.Info.Name = mMobs.colMobDB("MON" & Mob.ID_MOB).STR_NAME
                        struct2.Info.Level = mMobs.colMobDB("MON" & Mob.ID_MOB).INT2_LEVEL
                        struct2.Info.Avatar = mMobs.colMobDB("MON" & Mob.ID_MOB).AvatarImg


                        Socket.SockServer.SendData(Protocol.MakePacket(reply, struct2, Conn.Crypto), Conn.Index)
                        System.Windows.Forms.Application.DoEvents()

                    End If
                End If

            Next

            For Each npc As NPC.mNPCList.MirMapNPC In map.NPCs.lstNPCS

                If IsInsidePlayArea(x, y, npc.mX, npc.mY) Then

                    npcaddstruct.ID_NPC = npc.NPCID
                    npcaddstruct.Name = npc.mName
                    npcaddstruct.CMD = npc.mCMD
                    npcaddstruct.Shape = npc.mShape
                    npcaddstruct.X = npc.mX
                    npcaddstruct.Y = npc.mY

                    Socket.SockServer.SendData(Protocol.MakePacket(npcaddreply, npcaddstruct, Conn.Crypto), Conn.Index)

                End If

            Next

        End SyncLock

        'Dim i As Integer
        'For px As Integer = x - 2 To x + 2
        'For py As Integer = y - 2 To y + 2
        'i += 1
        'struct.ID_CHARACTER = 2 + i
        'struct.playerinfo = conn.PlayerInfo
        'struct.Positioninfo.BYTE_ANIM = HUMAnimation.Standing
        'struct.Positioninfo.BYTE_DIRECTION = Direction.S
        'struct.Positioninfo.INT2_X = px
        'struct.Positioninfo.INT2_Y = py
        'struct.playerinfo.ShapeInfo.INT_ARMORCOL = System.Drawing.Color.FromArgb(255, CInt(Rnd() * 254) + 1, CInt(Rnd() * 254) + 1, CInt(Rnd() * 254) + 1).ToArgb
        'struct.playerinfo.ShapeInfo.BOOL_HEADISHELM = CBool(CInt(Rnd()))
        'Socket.SockServer.SendData(Protocol.MakePacket(reply, struct, conn.Crypto), conn.Index)
        'Next
        'Next

    End Sub

    Friend Sub MobRemove(ByVal mob As MirMapMob)

        Dim remreply As Packet
        remreply.PacketType = GamePacketType.MobRemoveUpdate
        remreply.PacketOpCode = PacketOpCode.Notify

        Dim remstruct As MobRemoveInfo

        remstruct.ServerMobID = mob.ServerMobID

        SyncLock mob.map.objMapLock
            If mob.map.Players.Count > 0 Then

                For Each plyr As MirMapPlayer In mob.map.Players
                    If IsInsidePlayArea(mob.Pos.INT2_X, mob.Pos.INT2_Y, mob.Pos.INT2_X, mob.Pos.INT2_Y) Then
                        remreply.Username = plyr.conn.Username

                        Socket.SockServer.SendData(Protocol.MakePacket(remreply, remstruct, plyr.conn.Crypto), plyr.conn.Index)

                    End If
                Next

            End If

        End SyncLock

    End Sub

    Public Sub MobUpdatePlayers(ByRef mob As MirMapMob)

        On Error Resume Next

        If mob.ID_MOB = 0 Then Exit Sub

        If Not mMobs.colMobDB.Contains("MON" & mob.ID_MOB) Then Exit Sub

        Dim addreply As Packet
        addreply.PacketType = GamePacketType.MobAddUpdate
        Dim addstruct2 As MobAddInfo
        addstruct2.ServerMobID = mob.ServerMobID
        addstruct2.INT2_SHAPE = mMobs.colMobDB("MON" & mob.ID_MOB).INT2_SHAPE
        addstruct2.Positioninfo = mob.Pos
        addstruct2.VitalPoints = mob.VitalPoints

        addstruct2.Info.Name = mMobs.colMobDB("MON" & mob.ID_MOB).STR_NAME
        addstruct2.Info.Level = mMobs.colMobDB("MON" & mob.ID_MOB).INT2_LEVEL
        addstruct2.Info.Avatar = mMobs.colMobDB("MON" & mob.ID_MOB).AvatarImG


        Dim remreply As Packet
        remreply.PacketType = GamePacketType.MobRemoveUpdate

        Dim remstruct2 As MobRemoveInfo

        remstruct2.ServerMobID = mob.ServerMobID

        addreply.PacketOpCode = PacketOpCode.Notify
        remreply.PacketOpCode = PacketOpCode.Notify

        Dim map As MirMap = mob.map

        SyncLock map.objMapLock

            Dim x As Integer = mob.Pos.INT2_X
            Dim y As Integer = mob.Pos.INT2_Y

            Dim pkt As Packet
            Dim struct As MobUpdateInfo

            struct.ServerMobID = mob.ServerMobID
            struct.Positioninfo = mob.Pos
            struct.INT2_SHAPE = mMobs.colMobDB("MON" & mob.ID_MOB).INT2_SHAPE

            pkt.PacketType = GamePacketType.MobMoveUpdate
            pkt.PacketOpCode = PacketOpCode.Notify

            Dim movedir As Point = CalcNextStep(0, 0, mob.Pos.BYTE_DIRECTION)

            If map.Players.Count > 0 Then

                For Each plyr As MirMapPlayer In map.Players

                    If IsEnteringPlayArea(movedir, mob.Pos.INT2_X, mob.Pos.INT2_Y, plyr.conn.PositionInfo.INT2_X, plyr.conn.PositionInfo.INT2_Y) Then

                        addreply.Username = plyr.conn.Username
                        Socket.SockServer.SendData(Protocol.MakePacket(addreply, addstruct2, plyr.conn.Crypto), plyr.conn.Index)

                    ElseIf IsLeavingPlayArea(movedir, mob.Pos.INT2_X, mob.Pos.INT2_Y, plyr.conn.PositionInfo.INT2_X, plyr.conn.PositionInfo.INT2_Y) Then

                        remreply.Username = plyr.conn.Username
                        Socket.SockServer.SendData(Protocol.MakePacket(remreply, remstruct2, plyr.conn.Crypto), plyr.conn.Index)

                    Else

                        If IsInsidePlayArea(x, y, plyr.conn.PositionInfo.INT2_X, plyr.conn.PositionInfo.INT2_Y) Then

                            pkt.Username = plyr.conn.Username

                            Socket.SockServer.SendData(Protocol.MakePacket(pkt, struct, plyr.conn.Crypto), plyr.conn.Index)
                        End If
                    End If

                Next

            End If

        End SyncLock

    End Sub

    Public Sub MobSendToPlayers(ByRef mob As MirMapMob, ByVal pkt As Packet, ByVal struct As Object)

        SyncLock mob.map.objMapLock
            If mob.map.Players.Count > 0 Then

                For Each plyr As MirMapPlayer In mob.map.Players

                    If IsInsidePlayArea(mob.Pos.INT2_X, mob.Pos.INT2_Y, plyr.conn.PositionInfo.INT2_X, plyr.conn.PositionInfo.INT2_Y) Then

                        pkt.Username = plyr.conn.Username

                        Socket.SockServer.SendData(Protocol.MakePacket(pkt, struct, plyr.conn.Crypto), plyr.conn.Index)

                    End If
                Next

            End If

        End SyncLock

    End Sub

    Public Sub MobAddToPlayers(ByRef mob As MirMapMob)

        If mob.ID_MOB = 0 Then Exit Sub

        Dim addreply As Packet
        addreply.PacketType = GamePacketType.MobAddUpdate
        addreply.PacketOpCode = PacketOpCode.Notify
        Dim addstruct2 As MobAddInfo
        addstruct2.ServerMobID = mob.ServerMobID
        addstruct2.INT2_SHAPE = mMobs.colMobDB("MON" & mob.ID_MOB).INT2_SHAPE
        addstruct2.Positioninfo = mob.Pos
        addstruct2.VitalPoints = mob.VitalPoints
        addstruct2.Info.Name = mMobs.colMobDB("MON" & mob.ID_MOB).STR_NAME
        addstruct2.Info.Level = mMobs.colMobDB("MON" & mob.ID_MOB).INT2_LEVEL
        addstruct2.Info.Avatar = mMobs.colMobDB("MON" & mob.ID_MOB).AvatarImg


        SyncLock mob.map.objMapLock

            For Each plyr As MirMapPlayer In mob.map.Players

                If IsInsidePlayArea(mob.Pos.INT2_X, mob.Pos.INT2_Y, plyr.conn.PositionInfo.INT2_X, plyr.conn.PositionInfo.INT2_Y) Then

                    addreply.Username = plyr.conn.Username

                    Socket.SockServer.SendData(Protocol.MakePacket(addreply, addstruct2, plyr.conn.Crypto), plyr.conn.Index)

                End If

            Next

        End SyncLock

    End Sub

    Public Sub SendToAllInGame(ByVal pkt As Packet, ByVal struct As Object)

        For Each map As MirMap In mMaps.mMaps

            SyncLock map.objMapLock

                For Each player As MirMapPlayer In map.Players

                    pkt.Username = player.conn.Username
                    Socket.SockServer.SendData(Protocol.MakePacket(pkt, struct), player.conn.Index)

                Next

            End SyncLock

        Next

    End Sub

    Public Sub SendToLocalPlayers(ByRef Conn As Connection, ByVal pkt As Packet, ByVal struct As Object, Optional ByVal skipself As Boolean = False)

        Dim map As MirMap = GetMapFromConn(Conn)
        If map Is Nothing Then Exit Sub

        Dim x As Integer = Conn.PositionInfo.INT2_X
        Dim y As Integer = Conn.PositionInfo.INT2_Y

        
        SyncLock map.objMapLock

            For Each plyr As MirMapPlayer In map.Players
                If (plyr.conn.Index <> Conn.Index) Or (plyr.conn.Index = Conn.Index And Not skipself) Then
                    If IsInsidePlayArea(Conn.PositionInfo.INT2_X, Conn.PositionInfo.INT2_Y, plyr.conn.PositionInfo.INT2_X, plyr.conn.PositionInfo.INT2_Y) Then

                        pkt.Username = plyr.conn.Username

                        Socket.SockServer.SendData(Protocol.MakePacket(pkt, struct, plyr.conn.Crypto), plyr.conn.Index)
                        System.Windows.Forms.Application.DoEvents()

                    End If
                End If
            Next

        End SyncLock

    End Sub

    Public Sub SendToAllOnSameMap(ByRef Conn As Connection, ByVal pkt As Packet, ByVal struct As Object, Optional ByVal skipself As Boolean = False)

        Dim map As MirMap = GetMapFromConn(Conn)
        If map Is Nothing Then Exit Sub

        Dim x As Integer = Conn.PositionInfo.INT2_X
        Dim y As Integer = Conn.PositionInfo.INT2_Y

        SyncLock map.objMapLock

            For Each plyr As MirMapPlayer In map.Players
                If (plyr.conn.Index <> Conn.Index) Or (plyr.conn.Index = Conn.Index And Not skipself) Then

                    pkt.Username = plyr.conn.Username

                    Socket.SockServer.SendData(Protocol.MakePacket(pkt, struct, plyr.conn.Crypto), plyr.conn.Index)
                    System.Windows.Forms.Application.DoEvents()

                End If
            Next

        End SyncLock

    End Sub

    Public Sub SendToLocalPlayers(ByVal x As Integer, ByVal y As Integer, ByVal map As MirMap, ByVal pkt As Packet, ByVal struct As Object)

        SyncLock map.objMapLock

            For Each plyr As MirMapPlayer In map.Players
                If IsInsidePlayArea(x, y, plyr.conn.PositionInfo.INT2_X, plyr.conn.PositionInfo.INT2_Y) Then
                    pkt.Username = plyr.conn.Username

                    Socket.SockServer.SendData(Protocol.MakePacket(pkt, struct, plyr.conn.Crypto), plyr.conn.Index)
                    System.Windows.Forms.Application.DoEvents()

                End If
            Next

        End SyncLock

    End Sub

    Public Sub LoadPlayerLooks(ByRef Conn As Connection)


        Dim sSql As String = "SELECT a.ID_ITEM, a.INT_ALPHACOL, b.INT_SHAPE FROM TBL_ITEMS a JOIN TBL_ITEMBASE b ON a.ID_ITEMBASE = b.ID_ITEMBASE WHERE a.ID_CHARACTER = " & Conn.CharacterID & " AND (b.BYTE_TYPE = " & ItemType.MArmor & " OR b.BYTE_TYPE = " & ItemType.FArmor & ") AND a.BYTE_LOCATION = " & ItemLocation.EquipArmor & ";"

        'Dim sSql As String = "SELECT * FROM TBL_CHARACTEREQUIPMENT WHERE ID_CHARACTER=" & conn.CharacterID & ";"
        Dim row As Collection = DB.GameDB.ExecuteQueryFirstRecord(sSql)

        If row Is Nothing Then Exit Sub

        If row.Count > 0 Then

            Conn.PlayerInfo.ShapeInfo.INT_ARMORSHAPE = row("INT_SHAPE")
            Conn.PlayerInfo.ShapeInfo.INT_ARMORCOL = row("INT_ALPHACOL")

        End If

        sSql = "SELECT BYTE_HAIR, BYTE_HAIRCOL, BOOL_SHOWHELM FROM TBL_CHARACTER WHERE ID_CHARACTER=" & Conn.CharacterID & ";"
        Dim row2 As Collection = DB.GameDB.ExecuteQueryFirstRecord(sSql)

        If row2 Is Nothing Then Exit Sub

        If row2.Count > 0 Then

            Conn.PlayerInfo.ShapeInfo.BOOL_HEADISHELM = row2("BOOL_SHOWHELM")


            sSql = "SELECT a.ID_ITEM, b.INT_SHAPE FROM TBL_ITEMS a JOIN TBL_ITEMBASE b ON a.ID_ITEMBASE = b.ID_ITEMBASE WHERE a.ID_CHARACTER = " & Conn.CharacterID & " AND b.BYTE_TYPE = " & ItemType.Helmet & " AND a.BYTE_LOCATION = " & ItemLocation.EquipHelmet & ";"
            row = DB.GameDB.ExecuteQueryFirstRecord(sSql)
            If row Is Nothing Then Exit Sub

            If row.Count > 0 Then
                Conn.PlayerInfo.ShapeInfo.INT_HEADSHAPE = row("INT_SHAPE")
            Else
                Conn.PlayerInfo.ShapeInfo.BOOL_HEADISHELM = False
            End If

            Conn.PlayerInfo.ShapeInfo.INT_HAIRSHAPE = row2("BYTE_HAIR")
            Conn.PlayerInfo.ShapeInfo.INT_HAIRCOL = row2("BYTE_HAIRCOL")

            sSql = "SELECT a.ID_ITEM, b.INT_SHAPE FROM TBL_ITEMS a JOIN TBL_ITEMBASE b ON a.ID_ITEMBASE = b.ID_ITEMBASE WHERE a.ID_CHARACTER = " & Conn.CharacterID & " AND b.BYTE_TYPE = " & ItemType.Weapon & " AND a.BYTE_LOCATION = " & ItemLocation.EquipWeapon & ";"
            row = DB.GameDB.ExecuteQueryFirstRecord(sSql)

            If row Is Nothing Then Exit Sub

            If row.Count > 0 Then
                Conn.PlayerInfo.ShapeInfo.INT_WEAPONSHAPE = row("INT_SHAPE")
            Else
                Conn.PlayerInfo.ShapeInfo.INT_WEAPONSHAPE = -1
            End If

        End If


    End Sub

    Public Sub LoadPlayerInventory(ByRef Conn As Connection)

        Dim sSql As String = "SELECT ID_ITEM FROM TBL_ITEMS WHERE ID_CHARACTER=" & Conn.CharacterID & " AND BYTE_LOCATION=" & ItemLocation.Inventory & ";"
        Dim arr As ArrayList = DB.GameDB.ExecuteQuery(sSql)

        If arr Is Nothing Then Exit Sub
        If arr.Count > 0 Then

            Dim struct As ItemInfo

            Dim pkt As Packet
            pkt.PacketType = GamePacketType.AddToInventory
            pkt.PacketOpCode = PacketOpCode.Notify
            pkt.Username = Conn.Username

            For i As Integer = 0 To arr.Count - 1

                struct = ItemInfoFromItemID(arr(i)("ID_ITEM"))

                If Not struct.INT_ID = 0 Then

                    Socket.SockServer.SendData(Protocol.MakePacket(pkt, struct, Conn.Crypto), Conn.Index)

                    Conn.PlayerWeight.INT4_BAGWEIGHT += struct.Base.INT2_WEIGHT * struct.INT_COUNT
                    
                    System.Windows.Forms.Application.DoEvents()
                    Threading.Thread.Sleep(10)

                End If

            Next

            UpdatePlayerWeights(Conn)

        End If

    End Sub

    Public Sub LoadPlayerEquipment(ByRef Conn As Connection)

        Dim sSql As String = "SELECT ID_ITEM, BYTE_LOCATION FROM TBL_ITEMS WHERE ID_CHARACTER=" & Conn.CharacterID & " AND " & Globals.strEquipLocationSearch & ";"
        Dim arr As ArrayList = DB.GameDB.ExecuteQuery(sSql)

        If arr Is Nothing Then Exit Sub
        If arr.Count > 0 Then

            Dim struct As ItemSlotInfo

            '(re)initialize equiped items array
            ReDim Conn.EquipedItems(12)

            Dim pkt As Packet
            pkt.PacketType = GamePacketType.EquipItem
            pkt.PacketOpCode = PacketOpCode.Notify
            pkt.Username = Conn.Username

            For i As Integer = 0 To arr.Count - 1

                struct.item = ItemInfoFromItemID(arr(i)("ID_ITEM"))

                If Not struct.item.INT_ID = 0 Then

                    struct.slot = arr(i)("BYTE_LOCATION")

                    'EquipItemToConn(Conn, struct)
                    EquipItem(struct.item.INT_ID, struct.slot, Conn, True, True)

                    'Socket.SockServer.SendData(Protocol.MakePacket(pkt, struct, Conn.Crypto), Conn.Index)
                    'System.Windows.Forms.Application.DoEvents()

                    'AddItemStatsToConn(Conn, struct.item)

                End If

            Next

            UpdatePlayerStats(Conn)
            UpdatePlayersCharShape(Conn)

        End If

    End Sub

    Public Sub LoadBaseStats(ByRef Conn As Connection)

        Dim sSql As String = "SELECT * FROM TBL_DEFAULTSTATS WHERE INT2_LEVEL=" & Conn.Level & " AND BYTE_JOB=" & Conn.PlayerJob & ";"
        Dim row As Collection = DB.GameDB.ExecuteQueryFirstRecord(sSql)

        If row Is Nothing Then Exit Sub
        If row.Count > 0 Then

            Conn.VitalPoints.MAXHP = row("INT2_MAX_HP")
            Conn.VitalPoints.MAXMP = row("INT2_MAX_MP")

            Conn.RegenStats.INT8_HPAMMOUNT = row("INT8_HPREGENAMMOUNT")
            Conn.RegenStats.INT8_MPAMMOUNT = row("INT8_MPREGENAMMOUNT")
            Conn.RegenStats.INT8_HPSPEED = row("INT8_HPREGENTICK")
            Conn.RegenStats.INT8_MPSPEED = row("INT8_MPREGENTICK")
            Conn.RegenStats.INT8_HPTICK = DateTime.Now.Ticks
            Conn.RegenStats.INT8_HPTICK = DateTime.Now.Ticks

            Conn.Stats.INT2_MAX_AC = row("INT2_MAX_AC")
            Conn.Stats.INT2_MAX_MAC = row("INT2_MAX_MAC")
            Conn.Stats.INT2_MAX_DC = row("INT2_MAX_DC")
            Conn.Stats.INT2_MAX_SC = row("INT2_MAX_SC")
            Conn.Stats.INT2_MAX_MC = row("INT2_MAX_MC")

            Conn.Stats.INT2_MIN_AC = row("INT2_MIN_AC")
            Conn.Stats.INT2_MIN_MAC = row("INT2_MIN_MAC")
            Conn.Stats.INT2_MIN_DC = row("INT2_MIN_DC")
            Conn.Stats.INT2_MIN_SC = row("INT2_MIN_SC")
            Conn.Stats.INT2_MIN_MC = row("INT2_MIN_MC")

            Conn.Stats.INT1_ACCURACY = row("INT1_ACCURACY")
            Conn.Stats.INT1_AGILITY = row("INT1_AGILITY")
            Conn.Stats.INT1_LUCK = row("INT1_BODYLUCK")
            Conn.Stats.INT1_VELOCITY = row("INT1_VELOCITY")

            If Conn.IsGM Then
                Conn.Stats.INT2_MAX_WEIGHT = Int16.MaxValue
                Conn.Stats.INT2_MAX_HANDWEIGHT = Int16.MaxValue
                Conn.Stats.INT2_MAX_WEARWEIGHT = Int16.MaxValue
            Else
                Conn.Stats.INT2_MAX_WEIGHT = row("INT2_MAX_WEIGHT")
                Conn.Stats.INT2_MAX_HANDWEIGHT = row("INT2_MAX_HANDWEIGHT")
                Conn.Stats.INT2_MAX_WEARWEIGHT = row("INT2_MAX_WEARWEIGHT")
            End If


            Conn.Stats.INT2_ELEMAC_DARK = 0
            Conn.Stats.INT2_ELEMAC_FIRE = 0
            Conn.Stats.INT2_ELEMAC_HOLY = 0
            Conn.Stats.INT2_ELEMAC_ICE = 0
            Conn.Stats.INT2_ELEMAC_LIGHT = 0
            Conn.Stats.INT2_ELEMAC_PHANTOM = 0
            Conn.Stats.INT2_ELEMAC_WIND = 0

            Conn.Stats.INT2_ELEMC_DARK = 0
            Conn.Stats.INT2_ELEMC_FIRE = 0
            Conn.Stats.INT2_ELEMC_HOLY = 0
            Conn.Stats.INT2_ELEMC_ICE = 0
            Conn.Stats.INT2_ELEMC_LIGHT = 0
            Conn.Stats.INT2_ELEMC_PHANTOM = 0
            Conn.Stats.INT2_ELEMC_WIND = 0

        End If

    End Sub

    Public Sub UpdatePlayerStats(ByRef Conn As Connection)

        Dim pkt As Packet
        pkt.PacketType = GamePacketType.PlayerStatsUpdate
        pkt.PacketOpCode = PacketOpCode.Notify
        pkt.Username = Conn.Username

        Socket.SockServer.SendData(Protocol.MakePacket(pkt, Conn.Stats, Conn.Crypto), Conn.Index)

        System.Windows.Forms.Application.DoEvents()

    End Sub

    Public Sub UpdatePlayersCharShape(ByRef Conn As Connection)

        Dim pkt As Packet
        pkt.PacketType = GamePacketType.CharacterShapeUpdate
        pkt.PacketOpCode = PacketOpCode.Notify
        pkt.Username = DefaultUsername

        Dim struct As CharacterShape
        struct.ID_CHARACTER = Conn.CharacterID
        struct.info = Conn.PlayerInfo.ShapeInfo

        SendToLocalPlayers(Conn, pkt, struct, True)


    End Sub

    Public Sub PlayerDies(ByRef Conn As Connection)

        Dim pkt As Packet
        pkt.PacketType = GamePacketType.CharacterDie
        pkt.PacketOpCode = PacketOpCode.Notify
        pkt.Username = DefaultUsername

        Dim struct As CharacterID
        struct.ID_CHARACTER = Conn.CharacterID

        SendToLocalPlayers(Conn, pkt, struct)

        Conn.IsAlive = False

    End Sub

    'returns a string that identifies the character (name & ID) & account (username & ID) of the given Connection object
    Public Function GetCharAccString(ByVal Conn As Connection) As String
        Return "Character '" & Conn.PlayerInfo.STR_NAME & "'(" & Conn.CharacterID & ") of account '" & Conn.Username & "'(" & Conn.LoginID & ") "
    End Function

    Public Sub EquipItemToConn(ByVal conn As Connection, ByVal info As ItemSlotInfo)

        For i As Integer = 0 To 12

            If conn.EquipedItems(i).slot = info.slot Then
                conn.EquipedItems(i) = info
                Exit Sub
            End If

        Next

    End Sub

    Public Function IsInsidePlayArea(PlyrX As Integer, PlyrY As Integer, TargetX As Integer, TargetY As Integer) As Boolean

        If TargetX <= PlyrX + Constants.UpdateAreaX And TargetX >= PlyrX - Constants.UpdateAreaX And _
            TargetY <= PlyrY + Constants.UpdateAreaY And TargetY >= PlyrY - Constants.UpdateAreaY Then

            Return True

        Else

            Return False

        End If

    End Function

    Public Function IsInsideArea(PlyrX As Integer, PlyrY As Integer, TargetX As Integer, TargetY As Integer, size As Integer) As Boolean

        If TargetX <= PlyrX + size And TargetX >= PlyrX - size And _
            TargetY <= PlyrY + size And TargetY >= PlyrY - size Then

            Return True

        Else

            Return False

        End If

    End Function

    Public Function IsEnteringPlayArea(Dir As Point, PlyrX As Integer, PlyrY As Integer, TargetX As Integer, TargetY As Integer) As Boolean

        If Dir.X = -1 Then
            If TargetX = PlyrX - Constants.UpdateAreaX And TargetY >= PlyrY - Constants.UpdateAreaY And TargetY <= PlyrY + Constants.UpdateAreaY Then
                Return True
            End If
        ElseIf Dir.X = 1 Then
            If TargetX = PlyrX + Constants.UpdateAreaX And TargetY >= PlyrY - Constants.UpdateAreaY And TargetY <= PlyrY + Constants.UpdateAreaY Then
                Return True
            End If
        End If

        If Dir.Y = -1 Then
            If TargetY = PlyrY - Constants.UpdateAreaY And TargetX >= PlyrX - Constants.UpdateAreaX And TargetX <= PlyrX + Constants.UpdateAreaX Then
                Return True
            End If
        ElseIf Dir.Y = 1 Then
            If TargetY = PlyrY + Constants.UpdateAreaY And TargetX >= PlyrX - Constants.UpdateAreaX And TargetX <= PlyrX + Constants.UpdateAreaX Then
                Return True
            End If
        End If

        If Dir.X = -1 Then
            If Dir.Y = -1 Then
                If TargetX = PlyrX - Constants.UpdateAreaX And TargetY = PlyrY - Constants.UpdateAreaY Then
                    Return True
                End If
            ElseIf Dir.Y = 1 Then
                If TargetX = PlyrX - Constants.UpdateAreaX And TargetY = PlyrY + Constants.UpdateAreaY Then
                    Return True
                End If
            End If
        ElseIf Dir.X = 1 Then
            If Dir.Y = -1 Then
                If TargetX = PlyrX + Constants.UpdateAreaX And TargetY = PlyrY - Constants.UpdateAreaY Then
                    Return True
                End If
            ElseIf Dir.Y = 1 Then
                If TargetX = PlyrX + Constants.UpdateAreaX And TargetY = PlyrY + Constants.UpdateAreaY Then
                    Return True
                End If
            End If
        End If

        Return False

    End Function


    Public Function IsLeavingPlayArea(Dir As Point, PlyrX As Integer, PlyrY As Integer, TargetX As Integer, TargetY As Integer) As Boolean

        If Dir.X = -1 Then
            If TargetX = PlyrX + Constants.UpdateAreaX And TargetY >= PlyrY - Constants.UpdateAreaY And TargetY <= PlyrY + Constants.UpdateAreaY Then
                Return True
            End If
        ElseIf Dir.X = 1 Then
            If TargetX = PlyrX - Constants.UpdateAreaX And TargetY >= PlyrY - Constants.UpdateAreaY And TargetY <= PlyrY + Constants.UpdateAreaY Then
                Return True
            End If
        End If

        If Dir.Y = -1 Then
            If TargetY = PlyrY + Constants.UpdateAreaY And TargetX >= PlyrX - Constants.UpdateAreaX And TargetX <= PlyrX + Constants.UpdateAreaX Then
                Return True
            End If
        ElseIf Dir.Y = 1 Then
            If TargetY = PlyrY - Constants.UpdateAreaY And TargetX >= PlyrX - Constants.UpdateAreaX And TargetX <= PlyrX + Constants.UpdateAreaX Then
                Return True
            End If
        End If

        If Dir.X = -1 Then
            If Dir.Y = -1 Then
                If TargetX = PlyrX + Constants.UpdateAreaX And TargetY = PlyrY + Constants.UpdateAreaY Then
                    Return True
                End If
            ElseIf Dir.Y = 1 Then
                If TargetX = PlyrX + Constants.UpdateAreaX And TargetY = PlyrY - Constants.UpdateAreaY Then
                    Return True
                End If
            End If
        ElseIf Dir.X = 1 Then
            If Dir.Y = -1 Then
                If TargetX = PlyrX - Constants.UpdateAreaX And TargetY = PlyrY + Constants.UpdateAreaY Then
                    Return True
                End If
            ElseIf Dir.Y = 1 Then
                If TargetX = PlyrX - Constants.UpdateAreaX And TargetY = PlyrY - Constants.UpdateAreaY Then
                    Return True
                End If
            End If
        End If

        Return False

    End Function

    Public Sub UpdatePlayerHealth(conn As Connection, Optional ignoremiss As Boolean = False)

        Dim pkt As Packet
        Dim struct As CharacterHitInfo
        Dim struct2 As PlayerHitInfo

        pkt.PacketType = GamePacketType.CharacterHit
        pkt.PacketOpCode = PacketOpCode.Notify
        pkt.Username = DefaultUsername

        struct.CharacterID = conn.CharacterID
        struct.VitalPoints = conn.VitalPoints
        struct.IgnoreMiss = ignoremiss

        struct2.VitalPoints = conn.VitalPoints
        struct2.IgnoreMiss = ignoremiss


        SendToLocalPlayers(conn, pkt, struct, True)

        pkt.PacketType = GamePacketType.PlayerHit
        pkt.Username = conn.Username

        Socket.SockServer.SendData(Protocol.MakePacket(pkt, struct2, conn.Crypto), conn.Index)

    End Sub

    Public Sub LoadCurrency(conn As SocketServer.Connection)

        Dim sSql As String = "SELECT INT8_GOLD, INT8_GAMEGOLD, INT8_FAMEPOINTS FROM TBL_CHARACTER WHERE ID_CHARACTER=" & conn.CharacterID & ";"
        Dim row As Collection = DB.GameDB.ExecuteQueryFirstRecord(sSql)

        If Not row Is Nothing Then
            If row.Count > 0 Then

                conn.Gold = row("INT8_GOLD")
                conn.GameGold = row("INT8_GAMEGOLD")
                conn.FamePoints = row("INT8_FAMEPOINTS")

                RecieveGold(conn, 0)
                RecieveGameGold(conn, 0)
                RecieveFamePoints(conn, 0)

            End If
        End If

    End Sub

    Public Sub LoadExp(ByRef conn As SocketServer.Connection)

        Dim sSql As String = "SELECT INT2_LEVEL, INT8_EXP FROM TBL_BASESTATS WHERE ID_CHARACTER=" & conn.CharacterID & ";"
        Dim row As Collection = DB.GameDB.ExecuteQueryFirstRecord(sSql)

        If Not row Is Nothing Then
            If row.Count > 0 Then

                conn.Level = row("INT2_LEVEL")
                conn.Exp = row("INT8_EXP")

                sSql = "SELECT INT_EXP FROM TBL_LEVELS WHERE INT_LVL=" & conn.Level & ";"
                row = DB.GameDB.ExecuteQueryFirstRecord(sSql)

                If row Is Nothing Then
                    conn.ExpThisLvl = Long.MaxValue
                Else
                    If row.Count = 0 Then
                        conn.ExpThisLvl = Long.MaxValue
                    Else
                        conn.ExpThisLvl = row("INT_EXP")
                    End If
                End If

            End If
        End If

        UpdateExp(conn)

    End Sub

    Private Sub UpdateExp(conn As Connection)

        Dim struct As ExpInfo
        With struct
            .INT2_LEVEL = conn.Level
            .INT8_EXP = conn.Exp
            .INT8_TOTALEXP = conn.ExpThisLvl
        End With

        Dim pkt As Packet
        With pkt
            .PacketType = GamePacketType.SetExp
            .PacketOpCode = PacketOpCode.Notify
            .Username = conn.Username
        End With

        Dim sSql As String = "UPDATE TBL_BASESTATS SET INT8_EXP=" & conn.Exp & " WHERE ID_CHARACTER=" & conn.CharacterID & ";"
        DB.GameDB.ExecuteNonQuery(sSql)

        Socket.SockServer.SendData(MakePacket(pkt, struct, conn.Crypto), conn)



    End Sub

    Public Sub AddExp(conn As Connection, exp As Long)

        If conn.Level = Globals.ServerSettings.Max_Level Then
            Packets.SendSystemMessage(conn, "You have reached max level!")
            Exit Sub
        End If

        Dim addexp As Long = exp * Globals.ServerSettings.Rate_ExpMultiplier

        conn.Exp += addexp
        Packets.SendSystemMessage(conn, "Received " & addexp & " experience")

        Dim sSql As String

        If conn.Exp > conn.ExpThisLvl Then

            LevelUpMagic(conn)

            conn.Level += 1
            Packets.SendSystemMessage(conn, "Level is raised to " & conn.Level & "!")
            conn.Exp -= conn.ExpThisLvl
            sSql = "UPDATE TBL_BASESTATS SET INT2_LEVEL=" & conn.Level & ", INT8_EXP=" & conn.Exp & " WHERE ID_CHARACTER=" & conn.CharacterID & ";"
            DB.GameDB.ExecuteNonQuery(sSql)
            LoadExp(conn)

            LoadBaseStats(conn)
            LoadPlayerEquipment(conn)
            Application.DoEvents()
            UpdatePlayerStats(conn)
            conn.VitalPoints.HP = conn.VitalPoints.MAXHP
            conn.VitalPoints.MP = conn.VitalPoints.MAXMP
            UpdatePlayerHealth(conn)

        Else
            sSql = "UPDATE TBL_BASESTATS SET INT8_EXP=" & conn.Exp & " WHERE ID_CHARACTER=" & conn.CharacterID & ";"
            DB.GameDB.ExecuteNonQuery(sSql)
            UpdateExp(conn)
        End If


    End Sub

    Public Function HasGold(ByRef conn As Connection, ByVal ammount As Long)

        If conn.Gold >= ammount Then
            Return True
        Else
            Return False
        End If

    End Function

    Public Function TakeGold(conn As SocketServer.Connection, ammount As Long) As Boolean

        'If conn.Gold > ammount Then Return False

        conn.Gold -= ammount
        Dim sSql As String = "UPDATE TBL_CHARACTER SET INT8_GOLD=" & conn.Gold & " WHERE ID_CHARACTER=" & conn.CharacterID & ";"
        DB.GameDB.ExecuteNonQuery(sSql)

        Dim pkt As Packet
        pkt.PacketType = GamePacketType.SetCurrency
        pkt.PacketOpCode = EvoLib.Protocol.mProtocol.PacketOpCode.Notify
        pkt.Username = conn.Username
        
        Dim struct As CurrencyInfo
        struct.BYTE_TYPE = CurrencyType.Gold
        struct.INT8_TOTAL = conn.Gold
        struct.INT8_AMMOUNT = ammount

        Socket.SockServer.SendData(MakePacket(pkt, struct, conn.Crypto), conn)

        Return True

    End Function

    Public Function TakeGameGold(conn As SocketServer.Connection, ammount As Long) As Boolean

        'If conn.GameGold > ammount Then Return False

        conn.GameGold -= ammount
        Dim sSql As String = "UPDATE TBL_CHARACTER SET INT8_GAMEGOLD=" & conn.GameGold & " WHERE ID_CHARACTER=" & conn.CharacterID & ";"
        DB.GameDB.ExecuteNonQuery(sSql)

        Dim pkt As Packet
        pkt.PacketType = GamePacketType.SetCurrency
        pkt.PacketOpCode = EvoLib.Protocol.mProtocol.PacketOpCode.Notify
        pkt.Username = conn.Username
        
        Dim struct As CurrencyInfo
        struct.BYTE_TYPE = CurrencyType.GameGold
        struct.INT8_TOTAL = conn.GameGold
        struct.INT8_AMMOUNT = -ammount

        Socket.SockServer.SendData(MakePacket(pkt, struct, conn.Crypto), conn)

        Return True

    End Function

    Public Function TakeFamePoints(conn As SocketServer.Connection, ammount As Long) As Boolean

        If conn.FamePoints > ammount Then Return False

        conn.FamePoints -= ammount
        Dim sSql As String = "UPDATE TBL_CHARACTER SET INT8_FAMEPOINTS=" & conn.FamePoints & " WHERE ID_CHARACTER=" & conn.CharacterID & ";"
        DB.GameDB.ExecuteNonQuery(sSql)

        Dim pkt As Packet
        pkt.PacketType = GamePacketType.SetCurrency
        pkt.PacketOpCode = EvoLib.Protocol.mProtocol.PacketOpCode.Notify
        pkt.Username = conn.Username
        
        Dim struct As CurrencyInfo
        struct.BYTE_TYPE = CurrencyType.FamePoints
        struct.INT8_TOTAL = conn.FamePoints
        struct.INT8_AMMOUNT = -ammount

        Socket.SockServer.SendData(MakePacket(pkt, struct, conn.Crypto), conn)

        Return True

    End Function

    Public Sub RecieveGold(conn As SocketServer.Connection, ammount As Long)

        conn.Gold += ammount
        Dim sSql As String = "UPDATE TBL_CHARACTER SET INT8_GOLD=" & conn.Gold & " WHERE ID_CHARACTER=" & conn.CharacterID & ";"
        DB.GameDB.ExecuteNonQuery(sSql)


        Dim pkt As Packet
        pkt.PacketType = GamePacketType.SetCurrency
        pkt.PacketOpCode = EvoLib.Protocol.mProtocol.PacketOpCode.Notify
        pkt.Username = conn.Username
        
        Dim struct As CurrencyInfo
        struct.BYTE_TYPE = CurrencyType.Gold
        struct.INT8_TOTAL = conn.Gold
        struct.INT8_AMMOUNT = ammount


        Socket.SockServer.SendData(MakePacket(pkt, struct, conn.Crypto), conn)


    End Sub

    Public Sub RecieveGameGold(conn As SocketServer.Connection, ammount As Long)

        conn.GameGold += ammount
        Dim sSql As String = "UPDATE TBL_CHARACTER SET INT8_GAMEGOLD=" & conn.GameGold & " WHERE ID_CHARACTER=" & conn.CharacterID & ";"
        DB.GameDB.ExecuteNonQuery(sSql)

        Dim pkt As Packet
        pkt.PacketType = GamePacketType.SetCurrency
        pkt.PacketOpCode = EvoLib.Protocol.mProtocol.PacketOpCode.Notify
        pkt.Username = conn.Username
        
        Dim struct As CurrencyInfo
        struct.BYTE_TYPE = CurrencyType.GameGold
        struct.INT8_TOTAL = conn.GameGold
        struct.INT8_AMMOUNT = ammount

        Socket.SockServer.SendData(MakePacket(pkt, struct, conn.Crypto), conn)

    End Sub

    Public Sub RecieveFamePoints(conn As SocketServer.Connection, ammount As Long)

        conn.FamePoints += ammount
        Dim sSql As String = "UPDATE TBL_CHARACTER SET INT8_FAMEPOINTS=" & conn.FamePoints & " WHERE ID_CHARACTER=" & conn.CharacterID & ";"
        DB.GameDB.ExecuteNonQuery(sSql)

        Dim pkt As Packet
        pkt.PacketType = GamePacketType.SetCurrency
        pkt.PacketOpCode = EvoLib.Protocol.mProtocol.PacketOpCode.Notify
        pkt.Username = conn.Username
        
        Dim struct As CurrencyInfo
        struct.BYTE_TYPE = CurrencyType.FamePoints
        struct.INT8_TOTAL = conn.FamePoints
        struct.INT8_AMMOUNT = ammount

        Socket.SockServer.SendData(MakePacket(pkt, struct, conn.Crypto), conn)

    End Sub

    Public Sub UpdatePlayerWeights(conn As Connection)

        Dim pkt As Packet
        pkt.PacketType = GamePacketType.UpdatePlayerWeights
        pkt.PacketOpCode = PacketOpCode.Notify
        pkt.Username = conn.Username

        Dim struct As PlayerWeightDesc
        struct.ID_CHARACTER = conn.CharacterID
        struct.Weights = conn.PlayerWeight

        SendToLocalPlayers(conn, pkt, struct)

    End Sub

End Module